﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using mamnontuoitho7.AppCode;
using System.Data;
using System.IO;

namespace mamnontuoitho7.backend
{
    public partial class AlbumList : System.Web.UI.Page
    {

        private Album _album = new Album();
        private Product _product = new Product();
        private Member _mem = new Member();
        protected General _general = new General();
        protected DataTable _dt;
       
        protected void Page_Load(object sender, EventArgs e)
        {

            //Check Role Admin
            if (Session["memberID"].ToString() != "" && (ConstantClass.permissionArticle).IndexOf(Session["role"].ToString() + ",") != -1)
            {
                try
                {
                    if (Request.Form["hidProductID"] != null && Request.Form["hidVisible"] != null)
                    {
                        string _ProductID = Request.Form["hidProductID"].ToString();
                        string _Status = Request.Form["hidVisible"].ToString();
                        _product.ChangeProductStatus(_ProductID, _Status);
                    }

                    if (Request.Form["hidProductIDDelete"] != null)
                    {
                        string _ProductID = Request.Form["hidProductIDDelete"].ToString();
                        _product.ProductDelete(_ProductID);
                        /// delete image and thumbnail folder
                         string localfile = Request.PhysicalApplicationPath + "images/album/album-" + _ProductID;

                         string localfile2 = Request.PhysicalApplicationPath + "images/album/album-" + _ProductID + "/photo";

                         string localfile3 = Request.PhysicalApplicationPath + "images/album/album-" + _ProductID + "/photo/thumbnail";
                         //DirectoryInfo directory = new DirectoryInfo(localfile);
                         //System.Threading.Thread.Sleep(100);
                         //directory.Delete(true);

                         DirectoryInfo dir = new DirectoryInfo(localfile);
                         DirectoryInfo dir2 = new DirectoryInfo(localfile2);
                         DirectoryInfo dir3 = new DirectoryInfo(localfile3);

                         //xoa folder thumbnail và hinh
                         if (Directory.Exists(localfile3))
                         {
                             foreach (FileInfo files in dir3.GetFiles())
                             {
                                 files.Delete();
                             }
                             System.Threading.Thread.Sleep(100);
                             dir3.Delete(true);
                         }

                         //xoa folder photo và hình
                         if (Directory.Exists(localfile2))
                         {
                             foreach (FileInfo files in dir2.GetFiles())
                             {
                                 files.Delete();
                             }

                             System.Threading.Thread.Sleep(100);
                             if (!Directory.Exists(localfile3))
                             {
                                 dir2.Delete(true);
                             }
                         }

                         //xoa folder album-ID và hình
                         if (Directory.Exists(localfile))
                         {
                             foreach (FileInfo files in dir.GetFiles())
                             {
                                 files.Delete();
                             }

                             System.Threading.Thread.Sleep(100);
                             if (!Directory.Exists(localfile3))
                             {
                                 dir.Delete(true);
                             }
                         }

                        
                    }
                    if (!IsPostBack)
                    {
                        ddlAmountRecord.Items.Insert(0, new ListItem("50 Album Mới Nhất", "50"));
                        ddlAmountRecord.Items.Insert(1, new ListItem("Tất Cả Album", "0"));
                    }
                    _dt = _album.GetAlbum("admin", ddlAmountRecord.SelectedValue);
                }
                catch (IOException ex)
                {
                    Console.WriteLine(ex.Message);
                    Response.Redirect(ConstantClass._errorAdminPage);
                }

            }
            else
            {
                Response.Redirect(ConstantClass._errorLoginPage);
            }
        }

        protected void ddlAmountRecord_SelectedIndexChanged(object sender, EventArgs e)
        {
            try
            {
                _dt = _album.GetAlbum("admin", ddlAmountRecord.SelectedValue);
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
                Response.Redirect(ConstantClass._errorAdminPage);
            }
        }
    }
}